package com.example.yiwei.crm.json;

import com.example.yiwei.crm.entity.AreaEntity;
import com.example.yiwei.crm.util.NetWorkUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DHN on 2017/4/27.
 */

public class AreJson {
    public static List<AreaEntity> get(String url, String para){
        List<AreaEntity> list ;
        AreaEntity areaEntity;
        String result = NetWorkUtils.getCount(url,para);
        try {
            JSONObject object = new JSONObject(result);
            JSONArray array=object.getJSONArray("list");
            list = new ArrayList<>();
            for (int i=0;i<array.length();i++){
                areaEntity = new AreaEntity();
                JSONObject json=array.getJSONObject(i);
                if (json.has("name")){
                    areaEntity.setAreaname(json.getString("name"));
                }
                if (json.has("id")){
                    areaEntity.setAreaid(json.getString("id"));
                }
                if (json.has("type")){
                    areaEntity.setType(json.getString("type"));
                }
                list.add(areaEntity);
            }
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }
}
